// pages/users/wish/index/index.js
import {
  checkContent,
  message
} from '../../../../utils/util'
const {
  bbsPage,
  bbsLike,
  bbsLeave,
  bbsLeavePage,
  bbsMessagePage,
  bbsDelete,
  messageNum,
  readAllMessage
} = require('../../../../api/api')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    navHeight: getApp().globalData.navHeight,
    navTop: getApp().globalData.navTop,
    menuHeight: getApp().globalData.menuHeight,
    loading: true,
    dialogVisible: false,
    list: [
      // [{
      //   name: '奥特曼吃地瓜',
      //   content: '我要考清华，我要考北大！我全都要！',
      //   isPraise: false,
      //   isComment: false,
      //   avatar: 'https://ehsimg.oss-cn-beijing.aliyuncs.com/images/1675741158539.jpeg'
      // }, {
      //   name: '奥特曼吃地瓜',
      //   content: '我要考清华，我要考北大！我全都要！！！',
      //   isPraise: false,
      //   isComment: false,
      //   avatar: 'https://ehsimg.oss-cn-beijing.aliyuncs.com/images/1675741158539.jpeg'
      // }, {
      //   name: '奥特曼吃地瓜',
      //   content: '我要考清华，我要考北大！我全都要！！！',
      //   isPraise: false,
      //   isComment: false,
      //   avatar: 'https://ehsimg.oss-cn-beijing.aliyuncs.com/images/1675741158539.jpeg'
      // }, {
      //   name: '奥特曼吃地瓜',
      //   content: '我要考清华，我要考北大！我全都要！！！',
      //   isPraise: false,
      //   isComment: false,
      //   avatar: 'https://ehsimg.oss-cn-beijing.aliyuncs.com/images/1675741158539.jpeg'
      // }],
    ],
    allCount:0,
    triggered: false,
    isShowComment: false,
    bottomLift: getApp().globalData.bottomLift,
    number: 0,
    pageNo: 1,
    pageSize: 10,
    pageTo: 1,
    leaveList: [],
    currentIndex: 0,
    leftBg: 'https://ehsimg.oss-cn-beijing.aliyuncs.com/images/1683288909431.png',
    rightBg: 'https://ehsimg.oss-cn-beijing.aliyuncs.com/images/1683288927670.png',
    browseList: [[]
      // [{
      //   "courseTitle": "2023年多元升学途径之中外合作办学—林老师",
      //   "createTime": "2小时前",
      //   "nickname": "大明",
      //   "id": 133,
      //   "avatar": "https://ehsimg.oss-cn-beijing.aliyuncs.com/images/1676081464658.jpg",
      //   "courseId": 7,
      //   "userId": 455
      // }, {
      //   "courseTitle": "2023年多元升学途径之中外合作办学—林老师",
      //   "createTime": "6小时前",
      //   "nickname": "181****7562",
      //   "id": 130,
      //   "avatar": "https://ehsimg.oss-cn-beijing.aliyuncs.com/images/static/users/sex_boy.jpg",
      //   "courseId": 7,
      //   "userId": 943
      // }, {
      //   "courseTitle": "2023年多元升学途径之中外合作办学—林老师",
      //   "createTime": "01-20 19:22",
      //   "nickname": "133****5739",
      //   "id": 125,
      //   "avatar": "https://ehsimg.oss-cn-beijing.aliyuncs.com/images/static/users/sex_boy.jpg",
      //   "courseId": 7,
      //   "userId": 624
      // }, {
      //   "courseTitle": "2023年多元升学途径之中外合作办学—林老师",
      //   "createTime": "01-20 19:21",
      //   "nickname": "136****1887",
      //   "id": 122,
      //   "avatar": "https://ehsimg.oss-cn-beijing.aliyuncs.com/images/static/users/sex_boy.jpg",
      //   "courseId": 7,
      //   "userId": 922
      // }, {
      //   "courseTitle": "2023年多元升学途径之中外合作办学—林老师",
      //   "createTime": "01-20 19:20",
      //   "nickname": "浮云",
      //   "id": 119,
      //   "avatar": "https://thirdwx.qlogo.cn/mmopen/vi_32/niaPKOgBhGibnLjTSFuvhrnJv9nxMhI8fiaEZ4rTC4c0RUqhtbKFb2XMUWbq51g3gQ9dmnCzNtUGnTOKLWNSRIjTA/132",
      //   "courseId": 7,
      //   "userId": 453
      // }, {
      //   "courseTitle": "2023年多元升学途径之中外合作办学—林老师",
      //   "createTime": "01-20 19:17",
      //   "nickname": "176****0841",
      //   "id": 115,
      //   "avatar": "https://ehsimg.oss-cn-beijing.aliyuncs.com/images/static/users/sex_boy.jpg",
      //   "courseId": 7,
      //   "userId": 869
      // }, {
      //   "courseTitle": "2023年多元升学途径之中外合作办学",
      //   "createTime": "01-20 19:16",
      //   "nickname": "伏特加",
      //   "id": 112,
      //   "avatar": "https://ehsimg.oss-cn-beijing.aliyuncs.com/images/1678332793441.jpg",
      //   "courseId": 7,
      //   "userId": 555
      // }, {
      //   "courseTitle": "2023年多元升学途径之中外合作办学",
      //   "createTime": "01-20 19:16",
      //   "nickname": "158****8659",
      //   "id": 110,
      //   "avatar": "https://ehsimg.oss-cn-beijing.aliyuncs.com/images/static/users/sex_boy.jpg",
      //   "courseId": 7,
      //   "userId": 871
      // }, {
      //   "courseTitle": "2023年多元升学途径之中外合作办学",
      //   "createTime": "01-20 19:14",
      //   "nickname": "?",
      //   "id": 108,
      //   "avatar": "https://ehsimg.oss-cn-beijing.aliyuncs.com/images/1681342968040.jpg",
      //   "courseId": 7,
      //   "userId": 459
      // }, {
      //   "courseTitle": "2023年多元升学途径之中外合作办学",
      //   "createTime": "01-20 19:14",
      //   "nickname": "❌",
      //   "id": 106,
      //   "avatar": "https://ehsimg.oss-cn-beijing.aliyuncs.com/images/1675823130709.jpg",
      //   "courseId": 7,
      //   "userId": 449
      // }, {
      //   "courseTitle": "2023年多元升学途径之中外合作办学",
      //   "createTime": "01-20 19:14",
      //   "nickname": "132****9942",
      //   "id": 101,
      //   "avatar": "https://ehsimg.oss-cn-beijing.aliyuncs.com/images/1675741158539.jpeg",
      //   "courseId": 7,
      //   "userId": 604
      // }]
    ],
    browsePageNo: 1
  },
  openDialog() {
    this.setData({
      dialogVisible: true
    })
  },
  dialogClose() {
    this.setData({
      dialogVisible: false
    })
  },
  func() {

  },
  changeIndex(e) {
    const {
      index
    } = e.currentTarget.dataset
   
    if(index==1){
      this.setData({
        browsePageNo:1
      })
      // if(this.data.browseList[0].length>0)return
      this.loadMsg()
    }
    this.setData({
      currentIndex: index
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
   
    this.setData({
      triggered: true,
      userId: wx.getStorageSync('userInfo').id
    })
    this.loadData()
    this.getMessageNum()
    // this.getLeavePage()
  },
 
  // 获取许愿墙列表
  async loadData() {
    const {
      pageNo,
      pageSize,
    } = this.data
    const params = {
      pageNo,
      pageSize,
      isDelete: false,
    }
    const {
      result
    } = await bbsPage(params)
    result.records.map(e => {
      e.leaveList = []
      e.pageNo = 1
      // e.createBy = this.parseStringToStar(e.createBy)
    })
    this.setData({
      ['list[' + (pageNo - 1) + ']']: result.records,
      pageNo: pageNo + 1,
      triggered: false,
      // loading:false
    })
    this._freshing = false
  },
  //  获取我的消息未读数量
  async getMessageNum(){
    const  {result}=await messageNum()
      this.setData({
        allCount:result.allCount
      })
    },
  
  //消息列表
  async loadMsg() {
    this.getMessageNum()
    const {
      browsePageNo,
      pageSize,
    } = this.data
    const params = {
      pageNo:browsePageNo,
      pageSize,
      isDelete: false,
    }
    const {
      result
    } = await bbsMessagePage(params)
    this.setData({
      ['browseList[' + (browsePageNo - 1) + ']']: result.records,
      browsePageNo: browsePageNo + 1,
      triggered: false,
    })
    await readAllMessage()
    getApp().globalData.allCount=0
    this._freshing = false
  },
  //手机号转换
  parseStringToStar(str) {
    const isValidPhone = /^[1]([3-9])[0-9]{9}$/.test(str)
    return isValidPhone ? str.substr(0, 2) + new Array(str.length - 3).join('*') + str.substr(-2) : str
    // return str.length > 3 ? str.substr(0, 2) + new Array(str.length - 3).join('*') + str.substr(-2) : str
  },
  onRefresh() {
    if (this._freshing) return
    this._freshing = true
    setTimeout(res => {
      this.setData({
        pageNo: 1,
        triggered: true,
        list: []
      })
      this.loadData()

    }, 500)
  },
  onRefreshMsg() {
    if (this._freshing) return
    this._freshing = true
    // setTimeout(() => {
      this.setData({
        triggered: true,
      }, 1000)
      this._freshing = true
      this.setData({
        browsePageNo: 1,
        browseList: []
      })
      this.loadMsg()
    // }, 500)
  },
  loadMore() {
    const {
      list,
      pageSize
    } = this.data
    const count = list.length - 1
    if (list[count].length < pageSize) {
      return
    }
    this.loadData()
  },
  loadMoreMsg() {
    const {
      browseList,
      pageSize
    } = this.data
    const count = browseList.length - 1
    if (browseList[count].length < pageSize) {
      return
    }
    this.loadMsg()
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    return {
      title: '德道升学',
      imageUrl: 'https://ehsimg.oss-cn-beijing.aliyuncs.com/images/static/users/shareImg.jpg'
    }
  },
  goto(e) {
    const {
      tap
    } = e.currentTarget.dataset
    this.setData({
      isTap: true,
      tap: tap
    })
    setTimeout(() => {
      wx.navigateTo({
        url: '/pages/users/wish/form/index',
      })
    }, 100);
  },
  back() {
    var pages = getCurrentPages()
    if (pages.length > 1) {
      wx.navigateBack()
    } else {
      wx.switchTab({
        url: '/pages/index/index',
      })
    }
  },
  async praise(e) {
    const {
      index,
      pindex,
      id,
      ispraise
    } = e.currentTarget.dataset
    const {
      list
    } = this.data
    const {
      result,
      message
    } = await bbsLike({
      id
    })
    wx.showToast({
      title: message,
      icon: 'none'
    })
    list[pindex][index].isLike = result.status
    list[pindex][index].likeCount = result.likeCount
    this.setData({
      list
    })
  },

   deleteComment(e) {
    const {
      index,
      pindex,
      id,
      isLike
    } = e.currentTarget.dataset
    const {
      list
    } = this.data
    var _this = this
    wx.showModal({
      content: '确定删除该愿望吗~',
      confirmText: '删除',
      success: function (res) {
        console.log(res.confirm)
        if (res.confirm) {
          bbsDelete(id).then(()=>{
            wx.showToast({
              title: '删除成功',
              icon: 'none'
            })
            list[pindex].splice(index, 1)
            list[pindex][index].isDelete = true
            _this.setData({
              list,
            })
          })
          
        }
      }
    })

  },
  lookComment(e) {
    const {
      id,
      index,
      pindex
    } = e.currentTarget.dataset
    this.setData({
      commentId: id,
      commentIndex: index,
      commentPindex: pindex,
    })
    this.bbsLeavePage()
  },
  loadMoreComment(e) {
    const {
      id,
      index,
      pindex
    } = e.currentTarget.dataset
    const {
      list
    } = this.data
    if (list[pindex][index].leaveList.length >= list[pindex][index].total) {
      return
    }
    this.setData({
      commentId: id,
      commentIndex: index,
      commentPindex: pindex,
    })
    this.bbsLeavePage()
  },
  // async bbsLeavePage() {
  //   const {
  //     pageTo,
  //     pageSize,
  //     commentId,
  //     commentIndex,
  //     commentPindex,
  //     list
  //   } = this.data
  //   const data = list[commentPindex][commentIndex]
  //   const params = {
  //     pageNo: data.pageNo,
  //     pageSize: 3,
  //     id: commentId
  //   }
  //   const {
  //     result
  //   } = await bbsLeavePage(params)
  //   result.records.map((e)=>{
  //     e.createBy = this.parseStringToStar(e.createBy)
  //   })
  //   data.leaveList = [...data.leaveList, ...result.records]
  //   data.total = result.total
  //   data.pageNo = data.pageNo + 1
  //   this.setData({
  //     list
  //   })
  // },
  async bbsLeavePage() {
    const {
      pageTo,
      pageSize,
      commentId,
      commentIndex,
      commentPindex,
      list
    } = this.data
    const data = list[commentPindex][commentIndex]
    const params = {
      pageNo: 1,
      pageSize: 10000,
      id: commentId
    }
    const {
      result
    } = await bbsLeavePage(params)
    result.records.map((e) => {
      e.createBy = this.parseStringToStar(e.createBy)
    })
    data.msgList = [...result.records]
    data.total = result.total
    data.pageNo = data.pageNo + 1
    this.setData({
      list
    })
  },
  openComment(e) {
    const {
      id,
      index,
      pindex
    } = e.currentTarget.dataset
    this.setData({
      commentId: id,
      commentIndex: index,
      commentPindex: pindex,
      isShowComment: true
    })
  },
  hide() {
    this.setData({
      isShowComment: false,
      content: ''
    })
  },
  async callback() {
    const {
      content,
      commentId,
      commentIndex,
      commentPindex,
      list
    } = this.data
    if (!content) {
      wx.showToast({
        title: '请输入内容',
        icon: 'none'
      })
      return
    }
    checkContent(content).then(async (res) => {
      const {
        result,
        message
      } = await bbsLeave({
        id: commentId,
        content
      })
      const data = list[commentPindex][commentIndex]
      data.pageNo = 1
      data.leaveList = []
      data.leaveMsgCount = data.leaveMsgCount + 1
      wx.showToast({
        title: message,
        icon: 'none'
      })
      this.setData({
        isShowComment: false,
        content: '',
        list
      })
      this.bbsLeavePage()

    })

  },
  hideComment(){
    var temIds = ['OL-JNHv99hu7yKtaf3X1wC9HfmUcIN83r6Xul8UGXjw']
    let self = this;
    message(false, temIds, '', self.callback.bind(self))
  },

  //文本审核
  checkContent(e) {
    this.hideComment()
  },
  inputChange(e) {
    const value = e.detail.value
    this.setData({
      number: value.length,
      content: value
    })
  },
})